/**
 * 选座页面
 */
let vs = new Vue({
    el: '#seat-choose',
    data: {
        user: '',
        seats: [],
        ticket: {},
        place: [],
        seatIds: []
    },
    created() {
        let user = sessionStorage.getItem("user");
        if (user != null) {
            this.user = JSON.parse(sessionStorage.getItem("user"));

        }
        let formData = {
            showId: localStorage.getItem("showId"),

        }

        $.get('http://localhost:8080/cinema/api/SelectSeatsByShowIdServlet.do', formData, function (dto) {
            vs.seats = dto.value;

        });

        $.get('http://localhost:8080/cinema/api/SelectTicketInfoByShowIdServlet.do', formData, function (dto) {
            vs.ticket = dto.value;

        });

    },
    methods: {
        getSeat: function (r, c, id) {

            // 判断座位是否为已售状态
            if (this.seats[r - 1][c - 1].status == 2) {
                return;
            }

            var val = r + "排" + c + "座";


            // 判断是否重复选座，否则删除
            for (let i in this.seatIds) {
                if (this.seatIds[i] == id) {
                    this.seatIds.splice(i, 1);

                }
            }
            for (let i in this.place) {
                if (this.place[i] == val) {
                    this.place.splice(i, 1);
                    this.seats[r - 1][c - 1].status = 0;
                    return;
                }
            }



            // 判断选座数量是否已上限
            if (this.place.length >= 6) {
                $("#tip1").css("display", "block");
                return;
            }
            this.place.push(val);
            this.seatIds.push(id);
            this.seats[r - 1][c - 1].status = 1;



        },
        // 关闭购票上限提示框
        cls: function () {
            $("#tip1").css("display", "none");
        },
        log: function () {
            // let str = "";
            // for (let p of this.place) {
            //     str = str + " " + p;
            // }

            // let formData = {
            //     filmName: this.ticket.filmName,
            //     hallName: this.ticket.hallName,
            //     showDate: this.ticket.showDate,
            //     startTime: this.ticket.startTime,
            //     price: this.ticket.price * this.place.length,
            //     showId: this.ticket.showId,
            //     filmId: this.ticket.filmId,
            //     place: str,
            //     seatIdarr: this.seatIds
            // }

            // localStorage.setItem("LogWillAddOrder", JSON.stringify(formData));

            $("#tip2").hide();
            window.location.replace("../login/login.html");
        },
        no: function () {
            $("#tip2").hide();
        },
        buy: function () {
            // 判断是否选座
            if (this.place.length == 0) {
                return;
            }
            // 判断是否登录
            if (this.user == '') {
                $("#tip2").show();
                return;
            }

            // 添加订单前，先查询有没有未付款的订单，如果有，需先删除原先未付款的订单，并修改之前选中的座位状态为空
            $.get('http://localhost:8080/cinema/api/SelectOrderByStatusServlet.do', function (dto) {
                // 查询到有未付款的订单
                if (dto.code == 200) {
                    // 从本地存储取出数据 ，根据循环遍历的座位ID、以及放映ID，来修改座位状态
                    let currentSeat = JSON.parse(localStorage.getItem("currentSeat"));

                    // let formData2 = {
                    //     seatId: currentSeat.seatIdarr,
                    //     showId: currentSeat.showId,
                    //     status: 0
                    // }
                    // // 发送异步，实现根据放映ID、座位ID 修改放映场次对应影厅的座位状态
                    // $.ajax({
                    //     url: 'http://localhost:8080/cinema/api/UpdateSeatStatusByIdServlet.do',
                    //     type: 'post',
                    //     data: JSON.stringify(formData2),
                    //     dataType: 'json',
                    //     success: function (dto) {

                    //     }

                    // });

                    for (let sid of currentSeat.seatIdarr) {
                        let formData = {
                            seatId: sid,
                            showId: currentSeat.showId,
                            status: 0
                        }

                        // 发送异步，实现根据放映ID、座位ID 修改放映场次对应影厅的座位状态
                        $.ajax({
                            url: 'http://localhost:8080/cinema/api/UpdateSeatStatusByIdServlet.do',
                            type: 'put',
                            data: JSON.stringify(formData),
                            dataType: 'json',
                            success: function (dto) {

                            }

                        });
                    }
                }

                // 添加订单，并修改座位状态为已售
                let str = "";
                for (let p of vs.place) {
                    str = str + " " + p;
                }
                console.log(vs.ticket.filmName);
                let formData = {
                    filmName: vs.ticket.filmName,
                    hallName: vs.ticket.hallName,
                    showDate: vs.ticket.showDate,
                    startTime: vs.ticket.startTime,
                    price: vs.ticket.price * vs.place.length,
                    showId: vs.ticket.showId,
                    filmId: vs.ticket.filmId,
                    userId: vs.user.userId,
                    place: str
                }
                $.get('http://localhost:8080/cinema/api/OrderAddServlet.do', formData, function (dto) {
                    // let str2="";
                    // for(let sid of vs.seatIds){
                    //     str2=str2+" "+sid;
                    // }
                    // console.log(str2);

                    // // 根据"_"符号截取字符串
                    // // let arr=str.split(" ");

                    // // console.log(arr);

                    // let formData2 = {
                    //     seatId: str,
                    //     showId: vs.ticket.showId,
                    //     status: 2
                    // }
                    // console.log(formData2);
                    // // 发送异步，实现根据放映ID、座位ID 修改放映场次对应影厅的座位状态
                    // $.ajax({
                    //     url: 'http://localhost:8080/cinema/api/UpdateSeatStatusByIdServlet.do',
                    //     type: 'post',
                    //     data: JSON.stringify(formData2),
                    //     dataType: 'json',
                    //     success: function (dto) {
                    //         if(dto.code==200){
                    //             layer.msg(dto.value);
                    //         }else{
                    //             layer.msg(dto.msg);
                    //         }
                    //     }

                    // });

                    // 根据循环遍历的座位ID、以及放映ID，来修改座位状态
                    for (let sid of vs.seatIds) {
                        let formData2 = {
                            seatId: sid,
                            showId: vs.ticket.showId,
                            status: 2
                        }

                        // 发送异步，实现根据放映ID、座位ID 修改放映场次对应影厅的座位状态
                        $.ajax({
                            url: 'http://localhost:8080/cinema/api/UpdateSeatStatusByIdServlet.do',
                            type: 'put',
                            data: JSON.stringify(formData2),
                            dataType: 'json',
                            success: function (dto) {

                            }

                        });
                    }

                    // let str=vs.seatIds.join(',');
                    // let formData2={
                    //     seatId: vs.seatIds,
                    //     showId: vs.ticket.showId,
                    //     status: 2
                    // }
                    // $.ajax({
                    //     url: 'http://localhost:8080/cinema/api/UpdateSeatStatusByIdServlet.do',
                    //     type: 'post',
                    //     data: JSON.stringify(formData2),
                    //     dataType: 'json',
                    //     success: function (dto) {

                    //     }

                    // });

                    // 再将用户选中座位对应的座位ID，以及本场次的放映ID，存进本地存储中，用于后面修改座位状态的操作
                    let formData3 = {
                        seatIdarr: vs.seatIds,
                        showId: vs.ticket.showId
                    }
                    localStorage.setItem("currentSeat", JSON.stringify(formData3));
                    localStorage.setItem("orderId", dto.value);

                    $.cookie("djsendtime", null);
                    LockButton('#Time', 180);

                    location.href = 'home_pay.html';

                }, "json");


            }, "json");



        }
    }
});


















